package com.xgk.boot.framework.translate.query;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import java.util.function.Function;

@Slf4j
public class QueryFactory {

//    public static void setAndQuery(LambdaQueryWrapper queryWrapper, SimpleQueryParam simpleQueryParam,Class<?> clazz){
//        QueryParam queryParam = simpleQueryParam.getQueryParam();
//        ConditionEnum conditionEnum = queryParam.getCondition();
//        String property = queryParam.getAttribute();
//        Object value = queryParam.getValue();
////        Method method = getMethod(clazz, property);
//        Function<?, Object> fieldFunction = getGetterFunction(clazz, property);
//        if(fieldFunction == null){
//            log.warn("can't find {} method function",property);
//            return;
//        }
//        switch (conditionEnum){
//            case EQ:
//                queryWrapper.eq(fieldFunction,value);
//                break;
//            case NE:
//                break;
//            case GT:
//                queryWrapper.gt(property,value);
//                break;
//            case GE:
////                queryWrapper
//                break;
//            case LT:
//                break;
//            case LE:
//                break;
//            case IN:
//                break;
//            case LIKE:
//                break;
//            case IS_NULL:
//                break;
//            case NOT_NULL:
//                break;
//            case HAVING:
//                break;
//            case NOT_IN:
//                break;
//
//        }
//        OrderCondition orderCondition = simpleQueryParam.getOrderCondition();
//        if( orderCondition ==null){
//           return;
//        }
//        OrderEnum order = orderCondition.getOrder();
//        String propertie = orderCondition.getPropertie();
//        List<String> propertieList = orderCondition.getPropertieList();
//        switch (order){
//            case ASE:
//                if(StringUtils.isBlank(propertie) && !CollectionUtil.isEmpty(propertieList)){
//                    Object[] orders = propertieList.toArray();
//                    queryWrapper.orderByAsc(orders);
//                }else {
//                    queryWrapper.orderByAsc(propertie);
//                }
//                break;
//            case DESC:
//                if(StringUtils.isBlank(propertie) && !CollectionUtil.isEmpty(propertieList)){
//                    Object[] orders = propertieList.toArray();
//                    queryWrapper.orderByDesc(orders);
//                }else {
//                    queryWrapper.orderByDesc(propertie);
//                }
//                break;
//        }
//    }

    // 通过属性名称获取属性的 getter 方法对应的 Function
    public static <T> Function<T, Object> getGetterFunction(Class<T> clazz, String fieldName) {
        try {
            // 构造 isXxx 和 getXxx 方法名称
            String isGetterName = "is" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
            String getGetterName = "get" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);

            Method getterMethod;

            try {
                // 优先查找 isXxx 格式的 getter 方法
                getterMethod = clazz.getMethod(isGetterName);
            } catch (NoSuchMethodException e) {
                // 如果 isXxx 格式不存在，尝试 getXxx 格式
                getterMethod = clazz.getMethod(getGetterName);
            }
            // 返回一个 Function，传入对象后调用 getter 方法并返回结果
            Method finalGetterMethod = getterMethod;
            return (T obj) -> {
                try {
                    return finalGetterMethod.invoke(obj);
                } catch (Exception e) {
                    throw new RuntimeException("无法调用 getter 方法: " + finalGetterMethod.getName(), e);
                }
            };

        } catch (NoSuchMethodException e) {
            throw new RuntimeException("类中没有找到合适的 getter 方法: " + fieldName, e);
        }
    }

    // 通过属性名称获取属性对应的 Function
    public static <T> Function<T, Object> getFieldFunction(Class<T> clazz, String fieldName) {
        try {
            Field field = clazz.getDeclaredField(fieldName);
            field.setAccessible(true); // 设置为可访问

            // 返回一个 Function，传入对象后返回该属性值
            return (T obj) -> {
                try {
                    return field.get(obj);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException("无法访问属性: " + fieldName, e);
                }
            };

        } catch (NoSuchFieldException e) {
            throw new RuntimeException("类中没有找到属性: " + fieldName, e);
        }
    }

    public static Method getMethod(Class<?> clazz, String methodName) {
        Method[] methods = clazz.getMethods();
        for (Method method : methods) {
            if (method.getName().equals(methodName)) {
                return method;
            }
        }
        return null;
    }

    public static void setAndQuery(QueryWrapper queryWrapper, SimpleQueryParam simpleQueryParam){
        QueryParam queryParam = simpleQueryParam.getQueryParam();
        ConditionEnum conditionEnum = queryParam.getCondition();
        String property = queryParam.getAttribute();
        Object value = queryParam.getValue();
        switch (conditionEnum){
            case EQ:
                queryWrapper.eq(property,value);
                break;
            case NE:
                break;
            case GT:
                queryWrapper.gt(property,value);
                break;
            case GE:
//                queryWrapper
                break;
            case LT:
                break;
            case LE:
                break;
            case IN:
                break;
            case LIKE:
                break;
            case IS_NULL:
                break;
            case NOT_NULL:
                break;
            case HAVING:
                break;
            case NOT_IN:
                break;

        }
        OrderCondition orderCondition = simpleQueryParam.getOrderCondition();
        if( orderCondition ==null){
            return;
        }
        OrderEnum order = orderCondition.getOrder();
        String propertie = orderCondition.getPropertie();
        List<String> propertieList = orderCondition.getPropertieList();
        switch (order){
            case ASE:
                if(StringUtils.isBlank(propertie) && !CollectionUtil.isEmpty(propertieList)){
                    Object[] orders = propertieList.toArray();
                    queryWrapper.orderByAsc(orders);
                }else {
                    queryWrapper.orderByAsc(propertie);
                }
                break;
            case DESC:
                if(StringUtils.isBlank(propertie) && !CollectionUtil.isEmpty(propertieList)){
                    Object[] orders = propertieList.toArray();
                    queryWrapper.orderByDesc(orders);
                }else {
                    queryWrapper.orderByDesc(propertie);
                }
                break;
        }
    }

}
